//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.game;

import tv.dyndns.kishibe.client.packet.PacketProblemData;
import tv.dyndns.kishibe.client.util.SerialTimer;

import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HTML;

public class WidgetProblemSentenceEffect extends WidgetProblemSentenceNormal {
	private static final String TEMPLATE_PREFIX = "<p style='position:relative;'><span style='width:600px;height:64px;font-size:56px;text-align:center;vertical-align:middle;'>__hint__</span>";
	private static final String TEMPLATE_LINE = "<span style='position:absolute;left:__left__px;top:__top__px;background-color:#__color__;width:__width__px;height:64px;'></span>";
	private static final String TEMPLATE_SURFIX = "</p>";
	private static final int WIDTH = 600;
	private final HTML html = new HTML();
	private final String hint;
	private final String prefix;
	private static final int NUMBER_OF_LINE = 10;
	private static final int INITIALI_WIDTH = 100;
	private final int direction[] = new int[NUMBER_OF_LINE];
	private final int pos[] = new int[NUMBER_OF_LINE];
	private int count = 0;
	private static final String[] COLORS = { "FFFFFF", "F8F8F8", "F0F0F0", "E8E8E8", "E0E0E0", "D8D8D8", "D0D0D0", "C8C8C8", "C0C0C0", "B8B8B8", };
	private final Timer timer = new SerialTimer() {
		public void run() {
			update();
		}
	};

	public WidgetProblemSentenceEffect(PacketProblemData problem) {
		super(problem);
		setWidth("600px");

		hint = problem.choice[0];
		prefix = TEMPLATE_PREFIX.replaceAll("__hint__", hint);

		html.setPixelSize(600, 64);

		add(html);
		setCellHorizontalAlignment(html, ALIGN_CENTER);
		setCellVerticalAlignment(html, ALIGN_MIDDLE);
		setCellWidth(html, "600px");
		setCellHeight(html, "64px");

		for (int i = 0; i < NUMBER_OF_LINE; ++i) {
			direction[i] = Random.nextInt(10) + 6;
			if (Random.nextBoolean()) {
				direction[i] *= -1;
			}
			pos[i] = Random.nextInt(WIDTH - INITIALI_WIDTH);
		}

		update();
	}

	private void update() {
		if (++count > 150) {
			html.setHTML(prefix + TEMPLATE_SURFIX);
			return;
		}

		final int lineWidth = INITIALI_WIDTH * (300 - count) / 300;
		for (int i = 0; i < NUMBER_OF_LINE; ++i) {
			pos[i] += direction[i];
			if (pos[i] < 0) {
				direction[i] = Math.abs(direction[i]);
			} else if (pos[i] > WIDTH - lineWidth) {
				direction[i] = -Math.abs(direction[i]);
			}
		}

		final StringBuffer buffer = new StringBuffer();
		buffer.append(prefix);
		// final int topBase = html.getAbsoluteTop() + Controller.getInstance().getScrollPosition();
		// final int leftBase = html.getAbsoluteLeft() + Controller.getInstance().getHorizontalScrollPosition();
		final int topBase = 0;
		final int leftBase = 0;
		for (int i = 0; i < NUMBER_OF_LINE; ++i) {
			String line = TEMPLATE_LINE;
			line = line.replaceAll("__top__", "" + topBase);
			line = line.replaceAll("__left__", "" + (leftBase + pos[i]));
			line = line.replaceAll("__width__", "" + lineWidth);
			line = line.replaceAll("__color__", COLORS[i]);
			buffer.append(line);
		}
		buffer.append(TEMPLATE_SURFIX);

		html.setHTML(buffer.toString());
	}

	protected void onLoad() {
		super.onLoad();
		timer.scheduleRepeating(200);
	}

	protected void onUnload() {
		timer.cancel();
		super.onUnload();
	}
}
